package com.mokee.android.Login;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.mokee.android.SharedPreference.MyValuesInt;
import com.mokee.android.SharedPreference.MyValuesString;
import com.mokee.android.ShipPass.R;

public class RegisterActivity extends Activity implements OnClickListener {
	private static final String tag = "RegisterActivity";
	
	private EditText et_RegisterNickName, et_RegisterLoginName, et_RegisterPassward, et_RegisterPhone, et_RegisterIDCard,
					 et_RegisterShipName, et_RegisterWeightOri, et_RegisterRegNum;
	private Spinner sp_RegisterShipType;
	private TextView layout_sub_Title, iv_Submit;
	private LinearLayout layout_sub_Add;
	private Button btn_RegisterOK;
	
	private int shipType = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_register);
		
		initView();
	}

	private void initView() {
		et_RegisterNickName = (EditText) findViewById(R.id.et_RegisterNickName);
		et_RegisterLoginName = (EditText) findViewById(R.id.et_RegisterLoginName);
		et_RegisterPassward = (EditText) findViewById(R.id.et_RegisterPassward);
		et_RegisterPhone = (EditText) findViewById(R.id.et_RegisterPhone);
		et_RegisterIDCard = (EditText) findViewById(R.id.et_RegisterIDCard);
		et_RegisterShipName = (EditText) findViewById(R.id.et_RegisterShipName);
		sp_RegisterShipType = (Spinner) findViewById(R.id.sp_RegisterShipType);
		et_RegisterWeightOri = (EditText) findViewById(R.id.et_RegisterWeightOri);
		et_RegisterRegNum = (EditText) findViewById(R.id.et_RegisterRegNum);
		
		btn_RegisterOK = (Button) findViewById(R.id.btn_RegisterOK);
		
		layout_sub_Title = (TextView) findViewById(R.id.layout_sub_Title);
		iv_Submit = (TextView) findViewById(R.id.iv_Submit);
		layout_sub_Add = (LinearLayout) findViewById(R.id.layout_sub_Add);
		
		layout_sub_Title.setText("新用户注册");
		layout_sub_Add.setVisibility(View.GONE);
		
		btn_RegisterOK.setOnClickListener(this);
		sp_RegisterShipType.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				shipType = position;				
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) { }
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_RegisterOK:
			if(checkInputIsNull()){
				Log.i(tag, "船的类型为：" + shipType);
				
				MyValuesString.setNikeName(et_RegisterNickName.getText().toString().trim());
				MyValuesString.setLoginName(et_RegisterLoginName.getText().toString().trim());
				MyValuesString.setLoginPassward(et_RegisterPassward.getText().toString().trim());
				MyValuesString.setPhone(et_RegisterPhone.getText().toString().trim());
				MyValuesString.setIDCard(et_RegisterIDCard.getText().toString().trim());
				MyValuesString.setShipName(et_RegisterShipName.getText().toString().trim());
				MyValuesString.setRegisterNum(et_RegisterRegNum.getText().toString().trim());
				
				MyValuesInt.setShipType(shipType);
				MyValuesInt.setShipWeightOri(Integer.parseInt(et_RegisterWeightOri.getText().toString().trim()));
				
				Toast.makeText(getApplicationContext(), R.string.toast_register_submit_success, Toast.LENGTH_SHORT).show();
				
				finish();
				onDestroy();
			} else {
				Toast.makeText(this, R.string.toast_register_submit_fail, Toast.LENGTH_SHORT).show();
			}
			break;

		default:
			break;
		}
	}

	private boolean checkInputIsNull() {
		boolean result = false;
		if(et_RegisterNickName.getText().toString().trim().equals("")){
			return result;
		}
		if(et_RegisterLoginName.getText().toString().trim().equals("") || et_RegisterLoginName.getText().toString().trim().length() < 6){
			return result;
		}
		if(et_RegisterPassward.getText().toString().trim().equals("")){
			return result;
		}
		//检查手机号
		if(et_RegisterPhone.getText().toString().trim().length() < 11){
			return result;
		}
		//检查身份证号码
		if(et_RegisterIDCard.getText().toString().trim().length() < 18){
			return result;
		}
		if(et_RegisterShipName.getText().toString().trim().equals("")){
			return result;
		}
		if(et_RegisterWeightOri.getText().toString().trim().equals("")){
			return result;
		}
		if(et_RegisterRegNum.getText().toString().trim().equals("")){
			return result;
		}
		result = true;
		return result;
	}

}
